package net.maergaiyun.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
@Data
public class RabbitMQConfig {
    /**
     * 消息转换器
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // ========流量包处理: 用户初始化福利========
    /**
     * 交换机
     */
    private String trafficEventExchange = "traffic.event.exchange";

    /**
     * 用户注册免费流量包新增队列
     */
    private String trafficFreeInitQueue = "traffic.free_init.queue";

    /**
     * 用户注册免费流量包新增routing key
     */
    private String trafficFreeInitRoutingKey = "traffic.free_init.routing.key";

    /**
     * 创建交换机 Topic类型
     *
     * @return
     */
    @Bean
    public Exchange trafficEventExchange() {
        return new TopicExchange(trafficEventExchange, true, false);
    }

    /**
     * 队列绑定关系建立: 新用户注册免费流量包
     *
     * @return
     */
    @Bean
    public Binding trafficFreeInitBinding() {
        return new Binding(trafficFreeInitQueue, Binding.DestinationType.QUEUE, trafficEventExchange, trafficFreeInitRoutingKey, null);
    }

    /**
     * 免费流量包队列
     *
     * @return
     */
    @Bean
    public Queue trafficFreeInitQueue() {
        return new Queue(trafficFreeInitQueue, true, false, false);
    }


    // ========流量包扣减, 创建短链死信队列配置========
    // 发送锁定流量包消息->延迟交换机->延迟队列->死信交换机->死信队列
    /**
     * 延迟队列
     */
    private String trafficReleaseDelayQueue = "traffic.release.delay.queue";

    /**
     * 延迟队列routing key
     */
    private String trafficReleaseDelayRoutingKey = "traffic.release.delay.routing.key";

    /**
     * 死信队列
     */
    private String trafficReleaseQueue = "traffic.release.queue";

    /**
     * 死信队列routing key
     */
    private String trafficReleaseRoutingKey = "traffic.release.routing.key";

    /**
     * 过期时间, 1分钟
     */
    private Integer ttl = 60000;

    /**
     * 创建延迟队列
     *
     * @return
     */
    @Bean
    public Queue trafficReleaseDelayQueue() {
        Map<String, Object> args = new HashMap<>(3);
        args.put("x-message-ttl", ttl);
        args.put("x-dead-letter-exchange", trafficEventExchange);
        args.put("x-dead-letter-routing-key", trafficReleaseRoutingKey);
        return new Queue(trafficReleaseDelayQueue, true, false, false, args);
    }

    /**
     * 创建死信队列
     *
     * @return
     */
    @Bean
    public Queue trafficReleaseQueue() {
        return new Queue(trafficReleaseQueue, true, false, false);
    }

    /**
     * 建立延迟队列绑定关系
     *
     * @return
     */
    @Bean
    public Binding trafficReleaseDelayBinding() {
        return new Binding(trafficReleaseDelayQueue, Binding.DestinationType.QUEUE, trafficEventExchange, trafficReleaseDelayRoutingKey, null);
    }

    /**
     * 建立死信队列绑定关系
     *
     * @return
     */
    @Bean
    public Binding trafficReleaseBinding() {
        return new Binding(trafficReleaseQueue, Binding.DestinationType.QUEUE, trafficEventExchange, trafficReleaseRoutingKey, null);
    }
}
